import CryptoJS from "crypto-js";

const keyStr = "gEupJmPdeqzXIbWQMvHXhQopLhHgdc==";
const iv = "sF3UXsyfSgE4YA==";

export const MD5 = (str) => {
    return CryptoJS.MD5(str).toString();
};

/**
 * AES加密
 * @param plainText 明文
 * @returns {string} base64编码后的密文
 */
export function aesEncrypt(plainText) {
    let key = CryptoJS.enc.Utf8.parse(keyStr);
    let encrypted = CryptoJS.AES.encrypt(plainText, key, {
        iv: CryptoJS.enc.Utf8.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}

/**
 * AES解密
 * @param cipherText 密文
 * @return 明文
 */
export function aesDecrypt(cipherText) {
    let key = CryptoJS.enc.Utf8.parse(keyStr);
    let decrypted = CryptoJS.AES.decrypt(cipherText, key, {
        iv: CryptoJS.enc.Utf8.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
}
